<?php
class My_Common
{
    const STEP_DEFAULT = 86400; //1 NGAY
    const DB_OFFSET_DEFAULT = 0;
    const DB_LIMIT_DEFAULT = 20;
    const CLUSTER_ALL = 'all';
    const CCU_ASYNC_PERIOD = 300; //1 second. Day la thoi gian bat dpng bo giua cac server

    const USERNAME_KEY  = 'username';
    const USERID_KEY    = 'userId';

    const DATE_TYPE_STRING = 'string';
    const DATE_TYPE_INT = 'int';
    const DATE_FORMAT_DEFAULT = 'Y-m-d';

    const DISPLAY_MODE_SERVER = 'server';
    const DISPLAY_MODE_CHANNEL = 'channel';

    private static $_time; // current time of request
    private static $_yesterday; // yesterday time of request
    private static $_yesterdayFormated; // yesterday time of request
    private static $_config;
    public static function getCurrTime()
    {
        if(!self::$_time){
            self::$_time = time();
        }
        return self::$_time;
    }

    public static function getYesterdayTime($returnType = self::DATE_TYPE_STRING,
                                            $dateFormat = self::DATE_FORMAT_DEFAULT)
    {
        if(!self::$_yesterday){
            self::$_yesterday = strtotime('-1 days', mktime(0, 0, 0, date('m'), date('d'), date('Y')));
        }
        $dateFormat = isset($dateFormat[0]) ? $dateFormat : 'Y-m-d';

        //return  with input type
        if($returnType == self::DATE_TYPE_STRING){
            self::$_yesterdayFormated = date($dateFormat, self::$_yesterday);
            return self::$_yesterdayFormated;
        }else{
            return self::$_yesterday;
        }
    }

    public static function getDayInfo($date, &$startDate, &$endDate)
    {
        //validate date input
        if(!is_numeric($date)){
            $date = strtotime($date);
        }

        //parse date
        $baseYear   = date('Y', $date);
        $baseMonth  = date('m', $date);
        $baseDay    = date('j', $date); //current day

        //get begin of month
        $startDate = mktime(0, 0, 0, $baseMonth, $baseDay, $baseYear);
        $endDate = mktime(23, 59, 59, $baseMonth, $baseDay, $baseYear);
    }

    public static function getMonthInfo($date, &$startDate, &$endDate)
    {
        //validate date input
        if(isset($date[0])){
            $date = strtotime($date);
        }else{
            $date = self::getYesterdayTime();
        }

        //parse date
        $baseYear   = date('Y', $date);
        $baseMonth  = date('m', $date);
        $baseDay    = date('t', $date);

        //get begin of month
        $startDate = mktime(0, 0, 0, $baseMonth, 1, $baseYear);
        $endDate = mktime(23, 59, 59, $baseMonth, $baseDay, $baseYear);
    }
}